﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework; // Vector2 e demais classes para posicionamento.
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace first_project
{
    class Player
    {
        /// <summary>
        /// A imagem
        /// </summary>
        private Texture2D image;
        public Texture2D Image
        {
            get { return image; }
        }

        /// <summary>
        /// A imagem do tiro
        /// </summary>
        private Texture2D shotImage;
        public Texture2D ShotImage
        {
            get
            {
                return shotImage;
            }
            set
            {
                shotImage = value;
            }
        }
        
        /// <summary>
        /// A posicao
        /// </summary>
        private Vector2 position;
        public Vector2 Position
        {
            get 
            {
                return position; 
            }

            set 
            { 
                position = value; 
            }
        }

        private GameWindow window;

        /// <summary>
        /// A rotação
        /// </summary>
        private float rotation = 0f;

        /// <summary>
        /// A aceleracao
        /// </summary>
        private Vector2 acceleration = new Vector2(0.3f, 0.3f);
        public Vector2 Acceleration
        {
            get
            {
                return acceleration;
            }

            set
            {
                acceleration = value;
            }
        }

        private Vector2 speed = new Vector2(0, 0);

        private List<Shot> shotList;
        public List<Shot> ShotList
        {
            get
            {
                return this.shotList;
            }
        }

        public Rectangle CollisionRectangle
        {
            get
            {
                return new Rectangle((int)position.X, (int)position.Y, (int)(image.Width), (int)(image.Height));
            }
        }

        private double lastShotTime;
        private double shotIntervalTime;

        public Player(Texture2D image, Texture2D shotImage, GameWindow window)
        {
            this.image = image;
            this.shotImage = shotImage;
            this.window = window;
            this.shotList = new List<Shot>();
            this.lastShotTime = (double) -0.5;
            this.shotIntervalTime = (double) 0.5;
        }

        public void Update(GameTime gameTime)
        {
            processInputs(gameTime);
            movePalyer();
            processShots(gameTime);
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(image, position, null, Color.White, rotation, new Vector2(image.Width / 2, image.Height / 2), 1, SpriteEffects.None, 0);

            foreach (Shot shot in shotList)
            {
                shot.Draw(spriteBatch);
            }
        }

        // 
        // Método responsável pelo processamento das entradas(comandos).
        private void processInputs(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                rotation += 0.05f;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                rotation -= 0.05f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                speed += new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * acceleration;
                if (speed.X > 5)
                    speed.X = 5;
                else if (speed.X < -5)
                    speed.X = -5;
                if (speed.Y > 5)
                    speed.Y = 5;
                else if (speed.Y < -5)
                    speed.Y = -5;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if (gameTime.TotalGameTime.TotalSeconds.CompareTo(lastShotTime + shotIntervalTime) > 0)
                {
                    shotList.Add(new Shot(window, shotImage, new Vector2(position.X + image.Width / 2 * (float)Math.Cos(rotation), position.Y + image.Height / 2 * (float)Math.Sin(rotation)), new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation))));
                    lastShotTime = gameTime.TotalGameTime.TotalSeconds;
                }
            }
        }

        // Método responsável pelo movimento do player.
        private void movePalyer()
        {
            position += speed;
            if (position.X >= window.ClientBounds.Width + image.Width / 2)
            {
                position.X = -image.Width / 2;
            }
            else if (position.X <= -image.Width / 2)
            {
                position.X = window.ClientBounds.Width + image.Width / 2;
            }
            else if (position.Y >= window.ClientBounds.Height + image.Height / 2)
            {
                position.Y = -image.Height / 2;
            }
            else if (position.Y <= -image.Height / 2)
            {
                position.Y = window.ClientBounds.Height + image.Height / 2;
            }
        }

        // Método responsável pelo processamento dos tiros do player.
        private void processShots(GameTime gameTime)
        {
            List<Shot> removedShots = new List<Shot>();
            foreach (Shot shot in shotList)
            {
                shot.Update(gameTime);

                if (!shot.isActive)
                {
                    removedShots.Add(shot);
                }
            }

            foreach (Shot shot in removedShots)
            {
                shotList.Remove(shot);
            }
        }
    }
}
